Golang : Embedded or data bundling example
Golang complies your source code into a single executable file that allows your program end users to deploy the executable at target machine with ease. However, there are times when the executable needs to load external resources such as images or HTML templates to function properly (such as showing a splash screen) and this can make the downloading process cumbersome for your end users.
Other motivations could be that you want to optimize your Golang program execution time and do not want to slow down by loading external resource or you do not want your end users to modify the images or HTML templates that your program depends on.
There are couple of ways to put everything(embed/bundle) into a single executable file instead of having multiple files. Depending on your resources, the technique to handle text data for example, can be done with this method :
textData = `this is an example of bundling
text data in your program executable`
to print, use fmt.Println(textData)
As for bundling image, convert the image into base64 string ( see full example at encode image to base64 tutorial )
imgFile, err := os.Open("QrImgGA.png") // a QR code image
....
// convert the buffer bytes to base64 string - use buf.Bytes() for new image
imgBase64Str := base64.StdEncoding.EncodeToString(buf)
fmt.Println(imgBase64Str)
then you can store the imgBase64Str
string directly inside your source code and decode it back during runtime for display
imgFile, err := base64.StdEncoding.DecodeString(imgBase64Str)
or if your program is displaying the base64 encoded image directly to browser, then do this instead :
// Embed into an html without PNG file
img2html := "<html><body><img src=\"data:image/png;base64," + imgBase64Str + "\" /></body></html>"
Another option that you might want to explore is to use the go-bindata command line tool :
https://github.com/jteeuwen/go-bindata
Go-bindata converts any static file into a byte slice that can be embedded in your code.
NOTE :
Bear in mind that by bundling/embedding resource data into your executable might cause it to become large and also hard to debug/maintain in future by other people. Back in my old tech support days, I have to request help from the development team to look at the source code (written in C) for a very strange error message caused by bundled resource. Not an easy task if the original developer left the organisation long time ago and there's no written document on which resources are embedded in the executable.
References :
https://github.com/jteeuwen/go-bindata
https://www.socketloop.com/tutorials/golang-encode-image-to-base64-example
https://www.socketloop.com/tutorials/golang-web-javascript-to-server-side-websocket-example
See also : Golang : How to protect your source code from client, hosting company or hacker?
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+10.2k Golang : How to unmarshal JSON inner/nested value and assign to specific struct?
+7.2k Android Studio : AlertDialog to get user attention example
+10.8k Golang : Roll the dice example
+8.6k Golang : Find network service name from given port and protocol
+3.9k Javascript : Empty an array example
+6k Golang : How to search a list of records or data structures
+16.7k Golang : Get input from keyboard
+7.2k Golang : Rot13 and Rot5 algorithms example
+4.1k Javascript : How to show different content with noscript?
+4.9k Python : Create Whois client or function example
+17.7k Golang : Convert IPv4 address to decimal number(base 10) or integer
+12k Golang : 2 dimensional array example